package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.vo.Limit;
import com.zhentao.Exception.vo.ResultVo;
import com.zhentao.pojo.SysMenu;
import com.zhentao.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private SysMenuService sysMenuService;
//    添加菜单
    @RequestMapping("/addNume")
    public ResultVo addNune(@RequestBody SysMenu sysMenu){
        SysMenu sysMenu1 = new SysMenu();
//        添加标题
        sysMenu1.setTitle(sysMenu.getTitle());
//        添加内容
        sysMenu1.setComponent(sysMenu.getComponent());
        boolean save = sysMenuService.save(sysMenu1);
        return ResultVo.success(save);
    }
//
   @RequestMapping("/list")
    public ResultVo list(@RequestBody SysMenu sysMenu){
//        创建条件条件构造器
       QueryWrapper<SysMenu>queryWrapper = new QueryWrapper<>();
//       根据id进行查看
       queryWrapper.eq(sysMenu.getId()!=null,"id",sysMenu.getId());
       SysMenu one = sysMenuService.getOne(queryWrapper);
       System.err.println(one);
       return ResultVo.success(one);
   }
//   修改回显
   @RequestMapping("findById")
    public ResultVo findById(Integer id){
//        根据id进行菜单回显
       SysMenu byId = sysMenuService.getById(id);
       return ResultVo.success(byId);
   }
   @RequestMapping("update")
    public ResultVo update(@RequestBody SysMenu sysMenu){
       SysMenu byId = sysMenuService.getById(sysMenu.getId());
//        添加标题
       byId.setTitle(sysMenu.getTitle());
//        添加内容
       byId.setComponent(sysMenu.getComponent());
       boolean b = sysMenuService.updateById(byId);
       return ResultVo.success(b);
   }
//   查询所有的方法
   @RequestMapping("findMenu")
    public ResultVo findMenu(@RequestBody Limit limit){
       Page<SysMenu> page = sysMenuService.findPage(limit);
       return ResultVo.success(page);
   }

   // 更新菜单状态
   @RequestMapping("/updateStatus")
   public ResultVo updateStatus(@RequestBody SysMenu sysMenu) {
       log.info("接收到更新状态请求：id={}, status={}", sysMenu.getId(), sysMenu.getStatus());
       
       if (sysMenu.getId() == null) {
           log.error("菜单ID为空");
           return ResultVo.success(false);
       }
       
       try {
           SysMenu existingMenu = sysMenuService.getById(sysMenu.getId());
           if (existingMenu == null) {
               log.error("菜单不存在：id={}", sysMenu.getId());
               return ResultVo.success(false);
           }
           
           log.info("当前状态：{}, 目标状态：{}", existingMenu.getStatus(), sysMenu.getStatus());
           existingMenu.setStatus(sysMenu.getStatus());
           existingMenu.setUpdateTime(new Date());
           
           boolean success = sysMenuService.updateById(existingMenu);
           log.info("更新结果：{}", success);
           
           return ResultVo.success(success);
       } catch (Exception e) {
           log.error("更新状态时发生错误", e);
           return ResultVo.success(false);
       }
   }
}
